package com.zhangxi.line.util;

import java.security.MessageDigest;

/**
 * ClassName: MD5Util
 * Package: com.zhangxi.line.util
 */
public final class MD5Util {
    public static String encrypt(String strSrc) {
        try {
            //定义一个字符数组
            char hexChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
                    '9', 'a', 'b', 'c', 'd', 'e', 'f' };
            //将传入的字符串转换为一个字节数组
            byte[] bytes = strSrc.getBytes();
            //用于生成散列码
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            //使用 update 方法处理数据
            messageDigest.update(bytes);
            //调用 digest 方法,哈希计算并返回结果。
            bytes = messageDigest.digest();
            int j = bytes.length;
            //定义新数组长度为原字符的两倍
            char[] chars = new char[j * 2];
            int k = 0;
            for (int i = 0; i < bytes.length; i++) {
                byte b = bytes[i];
                chars[k++] = hexChars[b >>> 4 & 0xf];
                chars[k++] = hexChars[b & 0xf];
            }
            return new String(chars);
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("MD5加密出错！！+" + e);

        }
    }
}
